<template>
  <div class="playlist-page">
    <div class="page-header">
      <button class="back-btn" @click="back">
        <i class="fas fa-chevron-left"></i>
      </button>
      <h2>歌单</h2>
    </div>
    
    <div class="playlist-content">
      <div 
        v-for="playlist in playlists" 
        :key="playlist.play" 
        class="playlist-item"
        @click="selectPlaylist(playlist)"
      >
        <i class="fas fa-list"></i>
        <span>{{ getPlaylistDisplayName(playlist) }}</span>
        <div class="playlist-right">
          <!-- 当 nickname 不为空时显示 L 标识 -->
          <span v-if="playlist.nickname && playlist.nickname !== ''" class="nickname-indicator">L</span>
        </div>
        <i class="fas fa-chevron-right"></i>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { apiService } from '../utils/http'

export default {
  name: 'PlaylistPage',
  emits: ['select-playlist', 'back'],
  setup(props, { emit }) {
    const playlists = ref([])
    
    // 获取歌单显示名称
    const getPlaylistDisplayName = (playlist) => {
      return playlist.nickname || playlist.play
    }
    
    onMounted(async () => {
      try {
        const res = await apiService.getFresh('/QueryPlayList')
        playlists.value = res.data.playList
      } catch (error) {
        console.error('获取歌单失败:', error)
        // 模拟数据，保持与接口格式一致
        playlists.value = [
          { play: "舞曲", nickname: "" },
          { play: "1", nickname: "纯音乐" }
        ]
      }
    })
    
    // 现在传递完整的歌单对象
    const selectPlaylist = (playlist) => {
      emit('select-playlist', playlist)
    }
    
    const back = () => {
      emit('back')
    }
    
    return {
      playlists,
      getPlaylistDisplayName,
      selectPlaylist,
      back
    }
  }
}
</script>

<style scoped>
.playlist-page {
  min-height: 100vh;
  background: white;
  padding: 20px;
}

.page-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #e0e0e0;
}

.back-btn {
  background: none;
  border: none;
  font-size: 20px;
  color: #c62828;
  cursor: pointer;
  margin-right: 15px;
}

.page-header h2 {
  color: #333;
  font-size: 20px;
}

.playlist-content {
  display: flex;
  flex-direction: column;
}

.playlist-item {
  display: flex;
  align-items: center;
  padding: 15px 0;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: background 0.2s;
}

.playlist-item:hover {
  background: #f9f9f9;
}

.playlist-item i:first-child {
  font-size: 20px;
  color: #c62828;
  margin-right: 15px;
  width: 24px;
  text-align: center;
}

.playlist-item span {
  flex: 1;
  font-size: 16px;
  color: #333;
}

.playlist-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.nickname-indicator {
  background-color: #f8d0d0;
  color: white;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
}

.playlist-item i:last-child {
  color: #999;
  font-size: 14px;
}
</style>
